import dayjs from "dayjs"

/** 格式化时间 */
export const formatDateTime = (time: string | number | Date) => {
  if (!time) {
    return "N/A"
  }
  const date = new Date(time)
  return dayjs(date).format("YYYY-MM-DD HH:mm:ss")
}

/** 将全局 CSS 变量导入 JS 中使用 */
export const getCssVariableValue = (cssVariableName: string) => {
  let cssVariableValue = ""
  try {
    // 没有拿到值时，会返回空串
    cssVariableValue = getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
  } catch (error) {
    console.error(error)
  }
  return cssVariableValue
}

/*
格式化时间
  timestamp: 13位时间戳 | new Date() | Date()
  format => YY：年，M：月，D：日，H：时，m：分钟，s：秒，SSS：毫秒
*/
export const dateFormat = (timestamp: number | string | Date, format = "YYYY-MM-DD HH:mm:ss"): string => {
  const date = new Date(timestamp)
  function fixedTwo(value: number): string {
    return value < 10 ? "0" + value : String(value)
  }
  let showTime = format
  if (showTime.includes("SSS")) {
    const S = date.getMilliseconds()
    showTime = showTime.replace("SSS", "0".repeat(3 - String(S).length) + S)
  }
  if (showTime.includes("YY")) {
    const Y = date.getFullYear()
    showTime = showTime.includes("YYYY")
      ? showTime.replace("YYYY", String(Y))
      : showTime.replace("YY", String(Y).slice(2, 4))
  }
  if (showTime.includes("M")) {
    const M = date.getMonth() + 1
    showTime = showTime.includes("MM") ? showTime.replace("MM", fixedTwo(M)) : showTime.replace("M", String(M))
  }
  if (showTime.includes("D")) {
    const D = date.getDate()
    showTime = showTime.includes("DD") ? showTime.replace("DD", fixedTwo(D)) : showTime.replace("D", String(D))
  }
  if (showTime.includes("H")) {
    const H = date.getHours()
    showTime = showTime.includes("HH") ? showTime.replace("HH", fixedTwo(H)) : showTime.replace("H", String(H))
  }
  if (showTime.includes("m")) {
    const m = date.getMinutes()
    showTime = showTime.includes("mm") ? showTime.replace("mm", fixedTwo(m)) : showTime.replace("m", String(m))
  }
  if (showTime.includes("s")) {
    const s = date.getSeconds()
    showTime = showTime.includes("ss") ? showTime.replace("ss", fixedTwo(s)) : showTime.replace("s", String(s))
  }
  if (showTime.includes("ww")) {
    const date1 = new Date(date.getFullYear(), 0, 1)
    const date2 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 1)
    const dayMS = 24 * 60 * 60 * 1000
    const firstDay = (7 - date1.getDay()) * dayMS
    const weekMS = 7 * dayMS
    const timeDate1 = date1.getTime()
    const timeDate2 = date2.getTime()
    const w = Math.ceil((timeDate2 - timeDate1 - firstDay) / weekMS) + 1
    showTime = showTime.replace("ww", w + "")
  }
  return showTime
}

/**
 * 获取两日期之间日期列表函数
 * 返回两个时间之间所有的日期
 * 参数示例 （'2021-05-31','2021-06-30'）
 * **/
export const getTimeTwo = (start: any, end: any) => {
  //初始化日期列表，数组
  const diffdate = []
  let i = 0
  //开始日期小于等于结束日期,并循环
  while (start <= end) {
    diffdate[i] = start
    //获取开始日期时间戳
    const stime_ts = new Date(start).getTime()
    //增加一天时间戳后的日期
    const next_date = stime_ts + 24 * 60 * 60 * 1000
    //拼接年月日，这里的月份会返回（0-11），所以要+1
    const next_dates_y = new Date(next_date).getFullYear() + "-"
    const next_dates_m =
      new Date(next_date).getMonth() + 1 < 10
        ? "0" + (new Date(next_date).getMonth() + 1) + "-"
        : new Date(next_date).getMonth() + 1 + "-"
    const next_dates_d =
      new Date(next_date).getDate() < 10 ? "0" + new Date(next_date).getDate() : new Date(next_date).getDate()
    start = next_dates_y + next_dates_m + next_dates_d
    //增加数组key
    i++
  }
  return diffdate
}

export const dateUtil = { formatDateTime, getCssVariableValue, dateFormat, getTimeTwo }
